package com.yinhe.music.yhmusic.songmenu;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.android.exoplayer2.C;
import com.yinhe.music.common.utils.DisplayUtil;
import com.yinhe.music.yhmusic.R;
import com.yinhe.music.yhmusic.adapter.SongMenuContentAdapter;
import com.yinhe.music.yhmusic.base.BaseModel;
import com.yinhe.music.yhmusic.base.BasePresenter;
import com.yinhe.music.yhmusic.base.BaseServiceActivity;
import com.yinhe.music.yhmusic.cache.CacheManager;
import com.yinhe.music.yhmusic.constants.IConstants;
import com.yinhe.music.yhmusic.model.Music;
import com.yinhe.music.yhmusic.model.SongMenuList;
import com.yinhe.music.yhmusic.songmenu.contract.ISongMenuListContract;
import com.yinhe.music.yhmusic.songmenu.presenter.SongMenuListPresenter;
import com.yinhe.music.yhmusic.widget.CustomLoadMoreView;
import com.yinhe.music.yhmusic.widget.SpacesItemDecoration;
import com.yinhe.music.yhmusic.widget.WrapContentGridLayoutManager;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;


/**
 * 全部歌单列表
 *
 * @author 何晓华
 * @date 2017/8/17
 */

public class SongMenuListActivity extends BaseServiceActivity implements
        ISongMenuListContract.ISongMenuListView {
    @BindView(R.id.recommend_music_recyclerview)
    RecyclerView recyclerView;

    private SongMenuContentAdapter menuContentAdapter;
    private TextView hotMenu;
    private TextView newMenu;
    private TextView tvClassify;
    private List<String> tags = Arrays.asList("民歌", "古风", "弹唱", "清晨", "古道");
    private String sort = IConstants.NEW_SONG_MENU;

    private int mPageNum;

    SongMenuListPresenter mPresenter;

    @Override
    public void initView() {
        if (CacheManager.getInstance().getTypeLangId() == 0) {
            setToolbar("歌单");
        } else {
            setToolbar("曲调流派");
        }

        showQuickControl(true);
        initAdapter();
        mPage = 1;
        mPageSize = 18;
        reqMenuList();
    }

    @Override
    public int getLayoutResId() {
        return R.layout.activity_collection_radio;
    }

    @Override
    public BasePresenter getPresenter() {
        return mPresenter;
    }

    @Override
    public void initPresenter() {
        mPresenter = new SongMenuListPresenter();
        mPresenter.attachModelView(new BaseModel(), this);
    }

    /**
     * 初始化adapter
     */
    private void initAdapter() {
        menuContentAdapter = new SongMenuContentAdapter();
        menuContentAdapter.addHeaderView(getHeadView(getLayoutInflater()));

        menuContentAdapter.setOnItemClickListener((adapter, view, position) -> {
            SongMenuList menuList = (SongMenuList) adapter.getItem(position);
            if (menuList != null) startMenuInfo(menuList);
        });

        recyclerView.setLayoutManager(new WrapContentGridLayoutManager(this, 3));
        recyclerView.setAdapter(menuContentAdapter);

        menuContentAdapter.setLoadMoreView(new CustomLoadMoreView());
        menuContentAdapter.setOnLoadMoreListener(() -> {
            if (mPage <= mPageNum) {
                reqMenuList();
            } else {
                menuContentAdapter.loadMoreEnd();
            }
        }, recyclerView);

        menuContentAdapter.setPreLoadNumber(6);
        menuContentAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN);
    }

    /**
     * 跳转歌单详情
     *
     * @param menuList 歌单列表信息
     */
    private void startMenuInfo(SongMenuList menuList) {
        Intent intent = new Intent(this, SongMenuActivity.class);
        intent.putExtra("menu", menuList);
        intent.putExtra("menuid", menuList.getSongMenuId());
        intent.putExtra("menu", menuList);
        intent.putExtra("type", Music.Type.ONLINE);
        startActivity(intent);
    }

    /**
     * 请求歌单列表
     */
    private void reqMenuList() {
        mPresenter.getMenuList(mPage, mPageSize, sort);
    }

    /**
     * 热门分类头部
     */
    public View getHeadView(LayoutInflater inflater) {
        View view  = inflater.inflate(R.layout.fragment_song_menu, (ViewGroup) recyclerView.getParent(), false);
        hotMenu    = view.findViewById(R.id.hot_menu);
        newMenu    = view.findViewById(R.id.new_menu);
        tvClassify = view.findViewById(R.id.tv_classify);
        TextView tag1 = view.findViewById(R.id.tv_tag1);
        TextView tag2 = view.findViewById(R.id.tv_tag2);
        TextView tag3 = view.findViewById(R.id.tv_tag3);
        TextView tag4 = view.findViewById(R.id.tv_tag4);
        TextView tag5 = view.findViewById(R.id.tv_tag5);
        TextView tag6 = view.findViewById(R.id.tv_tag6);
        tag1.setText(tags.get(0));
        tag2.setText(tags.get(1));
        tag3.setText(tags.get(2));
        tag4.setText(tags.get(3));
        tag5.setText(tags.get(4));
        tag1.setOnClickListener(v -> showToast("tag1"));
        tag2.setOnClickListener(v -> showToast("tag2"));
        tag3.setOnClickListener(v -> showToast("tag3"));
        tag4.setOnClickListener(v -> showToast("tag4"));
        tag5.setOnClickListener(v -> showToast("tag5"));
        // 全部分类
        tag6.setOnClickListener(v -> startActivity(new Intent(this, SongMenuClassifyActivity.class)));
        tvClassify.setOnClickListener( v -> startActivity(new Intent(this, SongMenuClassifyActivity.class)));

        hotMenu.setOnClickListener(v -> reqHotList());
        newMenu.setOnClickListener(v -> reqNewList());

        return view;
    }

    /**
     * 请求最新列表
     */
    private void reqNewList() {
        newMenu.setTextColor(getResources().getColor(R.color.menu_sort_select));
        hotMenu.setTextColor(getResources().getColor(R.color.text_color_grey));
        sort = IConstants.NEW_SONG_MENU;
        mPage = 1;
        reqMenuList();
    }

    /**
     * 请求最热列表
     */
    private void reqHotList() {
        hotMenu.setTextColor(getResources().getColor(R.color.menu_sort_select));
        newMenu.setTextColor(getResources().getColor(R.color.text_color_grey));
        sort = IConstants.RECOMMEND_SONG_MENU;
        mPage = 1;
        reqMenuList();
    }

    @Override
    public void setSongMenuListUI(SongMenuList data) {
        List<SongMenuList> list = data.getSongMenuList();
        if (list == null) {
            return;
        }

        mPageNum = data.getPageNum();

        if (mPage == 1) {
            menuContentAdapter.setEmptyView(getView(R.layout.include_empty_layout, recyclerView));
        }

        setLoadMoreList(list, mPageNum, menuContentAdapter);
    }

    @Override
    public void setLoadMoreFailUI() {
        menuContentAdapter.loadMoreFail();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }
}
